package com.scau.hwadee.examination.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scau.hwadee.examination.entity.*;
import com.scau.hwadee.examination.mapper.*;
import com.scau.hwadee.examination.service.MajorCourseService;
import com.scau.hwadee.examination.utils.GetNumFromString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wrh
 * @date
 * @description
 */
@Service
public class MajorCourseServiceImpl implements MajorCourseService {
    @Autowired
    private MajorcourseMapper majorcourseMapper;

    @Autowired
    private CourseoptionalrelationMapper courseoptionalrelationMapper;

    @Autowired
    private AddexamcourseMapper addexamcourseMapper;

    @Autowired
    private CertificatereplacerelationMapper certificatereplacerelationMapper;

    @Autowired
    private CoursereplacerelationMapper coursereplacerelationMapper;

    @Autowired
    private MajorplanversionMapper majorplanversionMapper;

    private static Logger logger= LoggerFactory.getLogger(MajorCourseServiceImpl.class);


    @Override
    public int deleteMajorcourese(String mcId) {
        int cnt=majorcourseMapper.deleteByPrimaryKey(mcId);
        if(cnt<=0){
            logger.debug("删除失败");
        }
        else
            logger.debug("删除成功");
        return cnt;
    }

    @Override
    public int insertMajorcourse(Majorcourse record) {
        int cnt=majorcourseMapper.insertSelective(record);
        if(cnt<=0){
            logger.debug("添加失败");
        }
        else
            logger.debug("添加成功");
        return cnt;
    }

    @Override
    public Majorcourse selectMajorcourse(String mcId) {

        return majorcourseMapper.selectByPrimaryKey(mcId);
    }

    @Override
    public int updateMajorcourse(Majorcourse record) {
        int cnt= majorcourseMapper.updateByPrimaryKeySelective(record);
        if(cnt<=0){
            logger.debug("修改失败");
        }
        else
            logger.debug("修改成功");
        return cnt;
    }

    /**
     *
     * @param pageNum
     * @param pageSize
     * @param
     * @return
     * 根据专业查找专业课程
     */
    @Override
    public PageInfo<Majorcourse> selectAllMajorcourseByMajor(int pageNum, int pageSize, String mjId) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(majorcourseMapper.selectByMajor(pageNum, pageSize,mjId));
    }


    /**
     *
     * 专业选考相关功能
     */
    @Override
    public int addCourseoptionalrelation(Courseoptionalrelation courseoptionalrelation) {
        try{
            int cnt=courseoptionalrelationMapper.insertSelective(courseoptionalrelation);
            return cnt;
        }catch (Exception e){
             e.printStackTrace();
             return 0;

        }

    }

    @Override
    public List<Courseoptionalrelation> getCourseoptionalrelationByMajor(String mjId) {
        try{
            return courseoptionalrelationMapper.selectByMajor(mjId);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public int updateCourseoptionalrelation(Courseoptionalrelation courseoptionalrelation) {
        try{
            return courseoptionalrelationMapper.updateByPrimaryKeySelective(courseoptionalrelation);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }

    }

    /**
     * 专业加考相关功能
     * @param addexamcourse
     * @return
     */
    @Override
    public int addAddexamcourse(Addexamcourse addexamcourse) {
        try{
            return addexamcourseMapper.insertSelective(addexamcourse);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }

    }

    @Override
    public List<Addexamcourse> getAddexamcourseByMajorclass(String aceMajclass1) {
        try{
            return addexamcourseMapper.selectByMajorclass(aceMajclass1);
        }catch (Exception e){
           e.printStackTrace();
            return null;
        }
    }

    @Override
    public int updateAddexamcourse(Addexamcourse addexamcourse) {
        try{
            return addexamcourseMapper.updateByPrimaryKeySelective(addexamcourse);
        }catch (Exception e){
            e.printStackTrace();
            return  0;
        }
    }

       /**
     * 证书顶替相关功能
     */
     @Override
       public int addCertificatereplacerelation(Certificatereplacerelation certificatereplacerelation) {
         try{
             return certificatereplacerelationMapper.insertSelective(certificatereplacerelation);
         } catch (Exception e){
             e.printStackTrace();
             return  0;
         }
       }

     @Override
      public Certificatereplacerelation getCertificatereplacerelation(String ctName) {
         try {
             return certificatereplacerelationMapper.selectByctName(ctName);
         }catch (Exception e){
             e.printStackTrace();
             return null;
         }
       }

    @Override
    public List<Certificatereplacerelation> getAllCertificatereplacerelation() {
        return certificatereplacerelationMapper.selectAll();
    }

    /**
     *课程顶替功能
     *
     */
    @Override
    public int addCoursereplacerelation(Coursereplacerelation coursereplacerelation) {
        try{
            return coursereplacerelationMapper.insertSelective(coursereplacerelation);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public List<Coursereplacerelation> getCoursereplacerelationByMajor(String mjId, String mvId) {
        try{
            return  coursereplacerelationMapper.selectAllByMjMv(mjId ,mvId);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Coursereplacerelation> getLastCoursereplacerelation(String mjId, String mvId) {
        Majorplanversion majorplanversion = majorplanversionMapper.selectByPrimaryKey(mvId);
        Majorplanversion lastmajorplanversion=null;
        String str=majorplanversion.getMvName().trim();
        ArrayList list = GetNumFromString.getNumFromString(str);
        Integer num =(Integer) list.get(0);
        num--;

        String str1=(String) list.get(1);
        Integer num1=0;
        String str2 = "";
        List < Majorplanversion> allMajorplanversion=majorplanversionMapper.listAllMajorPlanVersion(1,9999999);
        //System.out.println(allMajorplanversion);
        for (int i=0;i<allMajorplanversion.size();i++){
            Majorplanversion m=allMajorplanversion.get(i);
            ArrayList list1= GetNumFromString.getNumFromString(m.getMvName().trim());
             num1=(Integer) list1.get(0);
            str2 = (String) list1.get(1);
            if(num.equals(num1) && str1.equals(str2))
            {
                lastmajorplanversion=m;
                break;
            }
        }
        //根据搜索到的mvid和mjId获取所有专业课程
        try{
            return getCoursereplacerelationByMajor(mjId,lastmajorplanversion.getMvId());
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }



}
